package com.klei.controller;

import com.klei.pojo.User;
import com.klei.service.IUserService;
import com.klei.util.LayuiJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/getAllUser")
    @ResponseBody
    public LayuiJson getAllUser(){
        LayuiJson layuiJson = new LayuiJson();

        layuiJson.setCode(0);
        layuiJson.setMsg("");
        layuiJson.setData(userService.getAllUser());

        return layuiJson;
    }

    @GetMapping("/getAllUserByPage")
    @ResponseBody
    public LayuiJson getAllUserByPage(@RequestParam(name = "page") int page,
                                      @RequestParam(name = "limit") int limit){
        LayuiJson layuiJson = new LayuiJson();

        layuiJson.setCode(0);
        layuiJson.setMsg("");
        layuiJson.setData(userService.getAllUserByPage((page - 1 ) * limit, limit));
        layuiJson.setCount(userService.getUserCount());

        return layuiJson;
    }

    @PostMapping("/saveUser")
    @ResponseBody
    public int saveUser(HttpServletRequest servletRequest, @RequestBody User user){

        User userByTel = userService.getUserByTel(user.getTel());

        // 0：注册成功  -1：注册失败  1：用户已被注册
        if (userByTel == null) {
            if (userService.saveUser(user)){
                HttpSession session = servletRequest.getSession();
                session.setAttribute("user", userService.getUserByTel(user.getTel()));
                return 0;
            } else {
                return -1;
            }
        }else {
            return 1;
        }

    }

    @PostMapping("/login")
    @ResponseBody
    public Boolean login(HttpServletRequest servletRequest, @RequestBody User user){

        User userByTAndP = userService.getUserByTAndP(user.getTel(), user.getPassword());

        if (userByTAndP == null) {
            return false;
        }

        HttpSession session = servletRequest.getSession();
        session.setAttribute("user", userByTAndP);
        return true;
    }

    @GetMapping("/getLoginUser")
    @ResponseBody
    public User getLoginUser(HttpServletRequest servletRequest){

        User user = (User) servletRequest.getSession().getAttribute("user");
        return user;
    }

    @PostMapping("/updateUser")
    @ResponseBody
    public boolean updateUser(HttpServletRequest servletRequest, @RequestBody User user){

        boolean flag = userService.updateUser(user);

        if (!flag) {
           return false;
        }

        User userByUid = userService.getUserByUid(user.getUid());
        HttpSession session = servletRequest.getSession();
        session.setAttribute("user", userByUid);
        return true;
    }

    @GetMapping("/updateUserPass/{uid}/{newPass}")
    @ResponseBody
    public boolean updateUserPass(@PathVariable("uid") int uid,
                                  @PathVariable("newPass") String newPass){

        return userService.updateUserPass(uid, newPass);
    }

    @DeleteMapping("/deleteUser/{uid}")
    @ResponseBody
    public boolean deleteUser(@PathVariable("uid") int uid){

        return userService.deleteUser(uid);
    }

}
